package com.customization.unionpay.market.workflowaction;

import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.dao.WorkflowRequestbaseDao;
import com.customization.unionpay.market.util.FormDataUtils;
import com.customization.unionpay.market.util.FormUtil;
import com.customization.unionpay.market.dao.ActivityApplicationDao;
import com.customization.unionpay.market.dao.ApplicationAdjustmentDao;
import com.engine.budget.service.impl.BusinessBudgetServiceImpl;
import weaver.general.BaseBean;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.OverTimeSetBean;
import weaver.workflow.request.RequestManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FinancialMattersOverAction extends BaseBean implements Action {

    private static final Logger log = LoggerFactory.getLogger(FinancialMattersOverAction.class);

    @Override
    public String execute(RequestInfo requestInfo) {
        try {
            RequestManager rm = requestInfo.getRequestManager();
            OverTimeSetBean overTimeSetBean = new OverTimeSetBean();
            int nodeId = rm.getNodeid();
            String nodeName = overTimeSetBean.getNodeName(nodeId);
            int formId = rm.getFormid();
            String loginid = rm.getUser().getLoginid();
            int requestid = rm.getRequestid();

            HashMap<String, Object> otherMap = new HashMap<>();
            otherMap.put("logId", requestid);
            otherMap.put("user", loginid);
            //获取主笔明细
            Map<String, Object> tableInfo = com.customization.unionpay.market.util.FormDataUtils.getMainFormData(requestInfo);
            String yxpthdsqm = tableInfo.get("yxpthdsqm").toString();
            ApplicationAdjustmentDao applicationAdjustmentDao = new ApplicationAdjustmentDao();
            Map<String, Object> adjustmentMap = applicationAdjustmentDao.queryBySqm(yxpthdsqm);
            com.customization.unionpay.market.dao.WorkflowRequestbaseDao workflowRequestbaseDao = new com.customization.unionpay.market.dao.WorkflowRequestbaseDao();
            BusinessBudgetServiceImpl businessBudgetService = new BusinessBudgetServiceImpl();
            ActivityApplicationDao activityApplicationDao = new ActivityApplicationDao();

            if (adjustmentMap != null && !adjustmentMap.isEmpty()) {
                releaseBudget(adjustmentMap, workflowRequestbaseDao, businessBudgetService, otherMap);
            } else {
                Map<String, Object> applicationMap = activityApplicationDao.queryBySqm(yxpthdsqm);
                if (applicationMap != null && !applicationMap.isEmpty()) {
                    releaseBudget(applicationMap, workflowRequestbaseDao, businessBudgetService, otherMap);
                }
            }

            return Action.SUCCESS;
        } catch (Exception e) {
            log.error("核验营销个税提计单异常", e);
            return Action.FAILURE_AND_CONTINUE;
        }
    }

    private void releaseBudget(Map<String, Object> map, WorkflowRequestbaseDao workflowRequestbaseDao, BusinessBudgetServiceImpl businessBudgetService, Map<String, Object> otherMap) {
        List<JSONObject> workflowDetails = workflowRequestbaseDao.queryById(Integer.parseInt(map.get("requestid").toString()));
        String zxrq = workflowDetails.get(0).getString("zxrq");

        map.put("zxrq", zxrq);
        map.put("lyxt", "智慧财务系统"); // 假设这里默认为 "智慧财务系统"
        map.put("zl", "SFZY");

        String ywlx = FormDataUtils.getItemName(FormUtil.APPLICATION_ADJUSTMENT_ID, "ywlx", map.get("ywlx").toString());
        if ("营销平台事项申请金额调增单".equals(ywlx)) {
            map.put("je", map.get("bcdzje"));
        } else {
            map.put("je", map.get("nbczje"));
        }
        businessBudgetService.execBudget(map, otherMap);
    }

//    private void processAdjustmentMap(Map<String, Object> adjustmentMap, WorkflowRequestbaseDao workflowRequestbaseDao, BusinessBudgetServiceImpl businessBudgetService,Map<String,Object> otherMap) {
//        Map<String, Object> map = prepareParams(adjustmentMap, workflowRequestbaseDao);
//        map.put("je", adjustmentMap.get("bcdzje"));
//        Map<String, Object> params = new HashMap<>();
//        params.put("params", Arrays.asList(map));
//        businessBudgetService.execBudget(params, otherMap);
//    }
//
//    private void processApplicationMap(Map<String, Object> applicationMap, WorkflowRequestbaseDao workflowRequestbaseDao, BusinessBudgetServiceImpl businessBudgetService,Map<String,Object> otherMap) {
//
//        Map<String, Object> map = prepareParams(applicationMap, workflowRequestbaseDao);
//        map.put("je", applicationMap.get("nbczje"));
//        Map<String, Object> params = new HashMap<>();
//        params.put("params", Arrays.asList(map));
//        businessBudgetService.execBudget(params, otherMap);
//    }

//    private Map<String, Object> prepareParams(Map<String, Object> map, WorkflowRequestbaseDao workflowRequestbaseDao) {
//        Map<String, Object> insertMap = new HashMap<>();
//        insertMap.put("ysxm", map.get("PROJECT_CODE"));
//        insertMap.put("yskm", map.get("yskm"));
//        insertMap.put("zxdw", map.get("yszrzx"));
//        insertMap.put("ysqjn", map.get("ysqjn"));
//        insertMap.put("zxdjh", map.get("bdh"));
//        insertMap.put("yskzq", map.get("yskzq"));
//        List<JSONObject> list = workflowRequestbaseDao.queryById(Integer.parseInt(map.get("requestid").toString()));
//        String zxrq = list.get(0).getString("zxrq");
//        insertMap.put("zxrq", zxrq);
//        insertMap.put("zhdjlx", map.get("ywlx"));
//        insertMap.put("bzr", map.get("sqr"));
//        insertMap.put("lyxt", "智慧财务系统");
//        insertMap.put("sm", map.get("sm"));
//        insertMap.put("bzbm", map.get("bzbm"));
//        insertMap.put("bzgs", map.get("bzgs"));
//        insertMap.put("jjsx", map.get("jjsx"));
//        insertMap.put("zl", "SFZY");
//        return insertMap;
//    }
}
